
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const stat = promisify(fs.stat);
const readdir = promisify(fs.readdir);
const handerbas = require('handlebars');
const mime = require('./mime');
const compress = require('./compress');
const acceptRange = require('./range');
const isCache = require('./cache'); 

const tplpath = path.join(__dirname, '../template/dir.tpl');
const source = fs.readFileSync(tplpath);
const template = handerbas.compile(source.toString());
const charset = 'utf8';

module.exports = async function (req, res, filePath, conf) {
    try {
        const stats = await stat(filePath);
        if(stats.isFile()){
            const contentType = mime(filePath) 
            res.setHeader('Content-Type', `${contentType};charset=${charset}`);

            if(isCache(stats, req, res)){
                res.statusCode = 304;
                res.end();
                return;
            }

            let rs;
            const {code, start, end} = acceptRange;
            if(code==200){
                res.statusCode = 200;
                rs= fs.createReadStream(filePath);
            } else{
                res.statusCode = 206;
                rs = fs.createReadStream(filePath, {start, end});
            }
            if(filePath.match(conf.compress)){
                rs = compress(rs, req, res);
            }
            rs.pipe(res);
        } else if(stats.isDirectory()){
            const files = await readdir(filePath);
            res.statusCode = 200;
            res.setHeader('Content-Type', 'text/html');
            const dir = path.relative(conf.root, filePath);
            const data = {
                title:path.basename(filePath),
                dir:dir?`/${dir}`:" ",
                files:files.map(file=>{
                    return {
                        file,
                        icon:mime(file)
                    };
                })
            };
            // res.end(dir.join(','));
            res.end(template(data));
            
        }
    } catch (error) {
        console.log(error);
        res.statusCode = 404;
        res.setHeader('Content-Type', 'text/plain');
        res.end(`${filePath} is not a dir or file\n ${error.toString()}`);
        return;
    }

}
